package controllers;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.DomainAnalyticModel;
import models.DomainModel;
import models.GoogleAdsenseModel;
import models.GoogleAnalyticModel;
import models.OutLinkModel;
import models.OwnerDomainModel;
import models.UserModel;

import data.DomainAnalyticData;
import data.DomainData;
import data.GoogleAdsenceData;
import data.GoogleAnalyticData;
import data.OutLinkData;
import data.OwnerDomainData;
import data.RecentSiteData;
import debugs.DomainAnalyticDebug;

public class DomainInfoServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		String path = ((HttpServletRequest) request).getRequestURI();
		String domainName = URLDecoder.decode(path.toLowerCase().replaceAll("/info/", ""),"UTF-8");
		
		if (domainName == null || domainName.isEmpty()) {
			response.sendRedirect("/message?txt=page-not-found");
			return;
		}
		
		DomainData domainData = new DomainData();
		DomainModel domain = domainData.get(domainName);
		
		if (domain == null) {
			response.sendRedirect("/message?txt=page-not-found");
			return;
		}
		
		if (domain.getStatus() == -1) {
			response.sendRedirect("/message?txt=blacklist");
			return;
		}
		
		OwnerDomainData ownerData = new OwnerDomainData();
		OwnerDomainModel ownerDomain = ownerData.getByDomain(domainName);
		if (ownerDomain != null && ownerDomain.getActive() == 0) {
			UserModel user = (UserModel) request.getAttribute("user");
			if (user == null || user.getUsername().isEmpty() || !ownerDomain.getUsername().equalsIgnoreCase(user.getUsername())) {
				response.sendRedirect("/message?txt=page-not-found");
				return;
			}
		}
		
		//Get google analytic
		GoogleAnalyticData googleAnalyticData = new GoogleAnalyticData();
		GoogleAnalyticModel googleAnalytic = googleAnalyticData.get(domain.getGoogleAnalyticId());
		
		//Get google adsence
		GoogleAdsenceData googleAdsenceData = new GoogleAdsenceData();
		GoogleAdsenseModel googleAdsense = googleAdsenceData.get(domain.getGoogleAdsenseId());
		
		//Get outlink
		OutLinkData oData = new OutLinkData();
		OutLinkModel outlink = oData.get(domain.getDomain());
		
		//Get domain analytic
		DomainAnalyticData domainAnalyticData = new DomainAnalyticData();
		DomainAnalyticModel domainAnalytic = domainAnalyticData.get(domainName);
		
		//Set into recentSiteUpdate
		RecentSiteData recentData = new RecentSiteData();
		recentData.setRecentView(domainName);
		
		request.setAttribute("domain", domain);
		request.setAttribute("domainAnalytic", domainAnalytic);
		request.setAttribute("googleAnalytic", googleAnalytic);
		request.setAttribute("googleAdsense", googleAdsense);
		request.setAttribute("outlink", outlink);
		try {
			request.getRequestDispatcher("/pages/domain_info.jsp").forward(request, response);
		} catch (ServletException e) {
			//e.printStackTrace();
		}
	}
}
